package com.ss.android.common.location;

import android.content.Context;
import android.os.Looper;
import android.os.Message;
import android.util.Base64;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.common.util.CommonConstants;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class LocationUploadHelper implements Callback {
    static final class f$a {
        int a;
        String b;
        String c;

        private f$a() {

        }

        f$a(byte arg1) {
            this();
        }
    }

    final LocationHelper a;
    final LocationGaoDeHelper b;
    final ServerLocationChangeHelper c;
    final BaseStationHelper d;
    final WeakHandler e;
    final Context f;
    boolean g;
    public boolean h;
    public boolean i;
    public long j;
    public long k;
    public int l;
    public boolean m;
    public boolean n;
    public int o;
    long p;
    private static LocationUploadHelper q;
    private static final WeakList<ServerLocationChangeHelper.a> r = new WeakList<>();

    private LocationUploadHelper(Context arg3) {
        this.h = true;
        this.i = true;
        this.l = 600;
        this.f = arg3;
        this.a = LocationHelper.getInstance(this.f);
        this.b = LocationGaoDeHelper.a(this.f);
        this.c = new ServerLocationChangeHelper(this.f);
        this.d = new BaseStationHelper(this.f);
        this.e = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
    }

    public static LocationUploadHelper a(Context arg3) {
        LocationUploadHelper v0_1;
        Class v1 = LocationUploadHelper.class;
        synchronized (v1) {
            if (q == null) {
                q = new LocationUploadHelper(arg3.getApplicationContext());
            }
            v0_1 = q;
        }
        return v0_1;
    }

    public final void a() {
        int v0;
        int v4 = 2;
        if (((this.h) || (this.i)) && ((NetworkUtils.isNetworkAvailable(this.f)) && !this.g)) {
            this.b();
            if (!this.n) {
                if (!this.h || (this.a.a(this.j))) {
                    if ((this.i) && !this.b.a(this.j)) {
                        v0 = 0;
                    }else {
                        v0 = 1;
                    }
                } else {
                    v0 = 0;
                }

                if (v0 != 0) {
                }else {
                    this.g = true;
                    this.e.sendEmptyMessage(1);
                    this.e.sendEmptyMessageDelayed(v4, 60000);
                    return;
                }
            }
            this.g = true;
            this.e.sendEmptyMessage(v4);
        }
    }

    public static void a(ServerLocationChangeHelper.a arg1) {
        if (arg1 != null) {
            r.add(arg1);
        }
    }

    static void a(JSONObject arg6) {
        if (arg6 != null) {
            try {
                long v0_1 = arg6.optLong("loc_time");
                if (v0_1 <= 0) {
                    return;
                }

                arg6.put("loc_time", v0_1 / 1000);
            } catch (Exception v0) {
            }
        }
    }

    public final boolean a(String arg7) {
        int v0 = -1;
        if (android.support.design.a.isTextEmpty(arg7)) {
            boolean v0_1 = false;
            return v0_1;
        }

        if (!NetworkUtils.isNetworkAvailable(this.f)) {
            return false;
        }

        JSONObject v2 = new JSONObject();
        try {
            v2.put("city_name", arg7);
        } catch (JSONException v0_2) {
            return false;
        }

        ArrayList v3 = new ArrayList();
        ((List) v3).add(new NameValueImpl("csinfo", b(v2)));
        int v2_1 = 20480;
        try {
            String v2_3 = NetworkUtils.executePost(v2_1, CommonConstants.USER_CITY_UPLOAD_URL, ((List) v3));
            if (android.support.design.a.isTextEmpty(v2_3)) {
            } else {
                v0 = new JSONObject(v2_3).optInt("err_no");
            }
        } catch (Exception v2_2) {
            Logger.d("LocationUploadHelper", "user city exception:" + v2_2.toString());
        }
        return v0 == 0 ? true : false;
    }

    public final void b() {
        this.g = false;
        this.e.removeCallbacksAndMessages(null);
    }

    public static void b(ServerLocationChangeHelper.a arg1) {
        if (arg1 != null) {
            r.remove(arg1);
        }
    }

    public static String b(JSONObject arg5) {
        String v0 = null;
        if (arg5.length() != 0) {
            try {
                byte[] v2 = arg5.toString().getBytes("UTF-8");
                int v3 = v2.length;
                int v1_1;
                for (v1_1 = 0; v1_1 < v3; ++v1_1) {
                    v2[v1_1] = ((byte) (v2[v1_1] ^ -99));
                }

                v0 = Base64.encodeToString(v2, 10);
            } catch (Exception v1) {
            }
        }

        return v0;
    }

    static void c() {
    }

    public final void d() {
        if (NetworkUtils.isNetworkAvailable(this.f)) {
            String v0 = this.c.a();
            if (!android.support.design.a.isTextEmpty(v0)) {
                new ThreadPlus(new i(v0), "user_loc_cancle_uplode", true).start();
            }
        }
    }

    public final void handleMsg(Message message) {
        switch (message.what) {
            case 1: {
                if (this.h) {
                    this.a.a(this.m);
                }

                if (!this.i) {
                    return;
                }

                this.b.a(this.m);
                break;
            }
            case 2: {
                long v0_1 = System.currentTimeMillis();
                this.k = v0_1;
                new ThreadPlus(new g(this, v0_1), "loc_uplode", true).start();
                break;
            }
            case 5: {
                if (!(message.obj instanceof LocationUploadHelper.f$a)) {
                    return;
                }

                Object v0 = message.obj;
                int v1 = ((LocationUploadHelper.f$a) v0).a;
                String v2 = ((LocationUploadHelper.f$a) v0).b;
                String v3 = ((LocationUploadHelper.f$a) v0).c;
                Iterator<ServerLocationChangeHelper.a> v4 = r.iterator();
                while (v4.hasNext()) {
                    v4.next().a(v1, v2, v3);
                }
            }
        }
    }
}

